//
//  TourDetailViewController.m
//  EasyToTesco2.0
//
//  Created by knew on 2017/5/4.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "TourDetailViewController.h"
#import "TourTableViewCell.h"
#import "TourModel.h"
#import "TourOrderTableViewController.h"

@interface TourDetailViewController ()<UITableViewDelegate,UITableViewDataSource,didClickSellerDetailPayButtonDelegate>

{
    TourDetailHeaderLayout *_headerLayout;
    TourSellerDetailCellHeaderLayout *_cellHeaderLayout;
}

@property (nonatomic,strong)NSMutableDictionary *detailDiction;

@property (nonatomic,strong)NSMutableArray *sellLayoutArray;

@property (nonatomic,strong)UITableView *tourTableView;


@end

@implementation TourDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self _initDetailResource];
    [self showHudInView:self.view hint:nil];
}

- (void)_initDetailResource {
    
    NSDictionary *params = @{
                             @"id":_replaceID
                             };
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"http://test.suntao-develop.com/api/travel/shangjia_detail" completeBlock:^(id object) {
        if (JudgeBackDataCode) {
            NSDictionary *dict = [object objectForKey:@"data"];
            TourDetailModel *model = [[TourDetailModel alloc] initWithDictionary:dict error:nil];
            NSLog(@"%@",model);
            TourDetailHeaderLayout *layout = [[TourDetailHeaderLayout alloc] _initDetailHeaderLayoutWithTourDetailModel:model];
            TourSellerDetailCellHeaderLayout *headerLayout = [[TourSellerDetailCellHeaderLayout alloc] _initDetailCellHeaderViewLayout];
            NSArray *product = model.products;
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSDictionary *dict in product) {
                TourProductsModel *model = [[TourProductsModel alloc] initWithDictionary:dict error:nil];
                TourDetailTableViewCellLayout *detailCellLayout = [[TourDetailTableViewCellLayout alloc] _initDetailCellLayoutWithTourProductsModel:model];
                [self.sellLayoutArray addObject:detailCellLayout];
                [tempArray addObject:model];
            }
            model.products = tempArray;
            _headerLayout = layout;
            _cellHeaderLayout = headerLayout;
            [self _initTourDetailHeaderView];
            [self _initTourDetailTableView];
            
        } else {
            
        }
    }];
}

- (void)didClickSellerDetailPayButton:(UIButton *)payButton {
    NSLog(@"%@",self.sellLayoutArray[payButton.tag]);
    [self showHudInView:self.view hint:nil];
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:@{@"id":[NSString stringWithFormat:@"%@",((TourDetailTableViewCellLayout *)self.sellLayoutArray[payButton.tag]).tourDetailModel.replaceID]} AndMenthodName:@"http://test.suntao-develop.com/api/travel/product_detail" completeBlock:^(id object) {
        
        NSDictionary *dict = [object objectForKey:@"data"];
        TourSellOrderModel *orderModel = [[TourSellOrderModel alloc] initWithDictionary:dict error:nil];
        
        TourSellOrderTableViewCellLayout *layout = [[TourSellOrderTableViewCellLayout alloc] _initTourSellOrderTableViewCellLayoutWithModel:orderModel];
        TourOrderLastViewLayout *lastView = [[TourOrderLastViewLayout alloc] _initTourOrderLastViewLayout];
        TourOrderTableViewController *order = [[TourOrderTableViewController alloc] init];
        order.tourOrderLastViewLayout = lastView;
        order.tourDetailHeaderLayout = _headerLayout;
        order.price = ((TourDetailTableViewCellLayout *)self.sellLayoutArray[payButton.tag]).tourDetailModel.yile_price;
        order.htmlString = layout.tourSellOrderModel.about;
        order.tourSellOrderTableViewCellLayout = layout;
        order.replaceID =  ((TourDetailTableViewCellLayout *)self.sellLayoutArray[payButton.tag]).tourDetailModel.replaceID;
        [self hideHud];
        [self.navigationController pushForm:self.navigationController pushToAndHidden:order];
        
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _headerLayout.detailModel.products.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kTourDetailTableViewCellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TourSellerDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tour"];
    if (!cell) {
        cell = [[TourSellerDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tour"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setTourSellerDetailCellWithLayout:self.sellLayoutArray[indexPath.row]];
    cell.PayButton.tag = indexPath.row;
    cell.delegate = self;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    TourSellerDetailCellHeader *headerView = [[TourSellerDetailCellHeader alloc] initWithFrame:CGRectMake(0, 0, WIDTH, kToueDetailTableViewHeaderHeight)];
    headerView.backgroundColor = BACKGROUND_COLOR;
    [headerView setHeaderViewLayout:_cellHeaderLayout];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kToueDetailTableViewHeaderHeight;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.tourTableView) {
        CGFloat sectionHeaderHeight = kToueDetailTableViewHeaderHeight; //sectionHeaderHeight
        
        if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
            
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
            
        }
    }
}


- (void)_initTourDetailHeaderView {
    TourSellerDetailView *detailHeader = [[TourSellerDetailView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT * 0.2)];
    [detailHeader setDetailHeaderViewWithModel:_headerLayout];
    detailHeader.phoneButtonBlock = ^() {
        
        NSString *phoneNumStr = [NSString stringWithFormat:@"tel://%@",_headerLayout.detailModel.phone];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumStr]];
        
    };
    [self.view addSubview:detailHeader];
}

- (void)_initTourDetailTableView {
    self.tourTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT * 0.2, WIDTH, HEIGHT - kNavigationBarHeight - HEIGHT * 0.2) style:UITableViewStylePlain];
    self.tourTableView.delegate = self;
    self.tourTableView.dataSource = self;
    self.tourTableView.tableFooterView = [[UIView alloc] init];
    //    self.tourTableView.showsHorizontalScrollIndicator = NO;
    self.tourTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.tourTableView];
    [self hideHud];
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

- (NSMutableDictionary *)detailDiction {
    if (!_detailDiction) {
        _detailDiction = [NSMutableDictionary dictionary];
    }
    return _detailDiction;
}

- (NSMutableArray *)sellLayoutArray {
    if (!_sellLayoutArray) {
        _sellLayoutArray = [NSMutableArray array];
    }
    return _sellLayoutArray;
}


@end
